package com.pzl.arcgis;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PolygonBuilder;
import com.esri.arcgisruntime.geometry.PolylineBuilder;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;

public class RendererActivity extends AppCompatActivity {

    private MapView mMapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_renderer);

        // create MapView from layout
        mMapView = (MapView) findViewById(R.id.mapView);
        // create a map with the Basemap Type topographic
        ArcGISMap mMap = new ArcGISMap(Basemap.Type.TOPOGRAPHIC, 15.169193, 16.333479, 2);
        // add graphics overlay
        addGraphicsOverlay();
        // set the map to be displayed in this view
        mMapView.setMap(mMap);
    }

    private void addGraphicsOverlay() {

        addPointOverLay();

        addLineOverLay();

        addPolygonOverLay();
    }

    /**
     * 添加多边形
     */
    private void addPolygonOverLay() {
        //polygon graphic
        PolygonBuilder polygonGeometry = new PolygonBuilder(SpatialReferences.getWgs84());
        polygonGeometry.addPoint(121.355072,31.160913);
        polygonGeometry.addPoint(121.353796,31.163215);
        polygonGeometry.addPoint(121.358683,31.165424);
        polygonGeometry.addPoint(121.360641,31.162643);
        // solid yellow polygon symbol
        SimpleFillSymbol polygonSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.FORWARD_DIAGONAL, Color.YELLOW, null);
        // create graphic for polygon
        Graphic polygonGraphic = new Graphic(polygonGeometry.toGeometry());
        // create graphic overlay for polygon
        GraphicsOverlay polygonGraphicOverlay = new GraphicsOverlay();
        // create simple renderer
        SimpleRenderer polygonRenderer = new SimpleRenderer(polygonSymbol);
        // add graphic to overlay
        polygonGraphicOverlay.setRenderer(polygonRenderer);
        // add graphic to overlay
        polygonGraphicOverlay.getGraphics().add(polygonGraphic);
        // add graphics overlay to MapView
        mMapView.getGraphicsOverlays().add(polygonGraphicOverlay);
    }


    /**
     *
     */
    private void addLineOverLay() {
        // line graphic
        PolylineBuilder lineGeometry = new PolylineBuilder(SpatialReferences.getWgs84());
        lineGeometry.addPoint(113.170224, 28.040329);
        lineGeometry.addPoint(121.358849, 31.162929);
        // solid blue line symbol
        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH_DOT, Color.BLUE, 2);
        // create graphic for polyline
        Graphic lineGraphic = new Graphic(lineGeometry.toGeometry());
        // create graphic overlay for polyline
        GraphicsOverlay lineGraphicOverlay = new GraphicsOverlay();
        // create simple renderer
        SimpleRenderer lineRenderer = new SimpleRenderer(lineSymbol);
        // add graphic to overlay
        lineGraphicOverlay.setRenderer(lineRenderer);
        // add graphic to overlay
        lineGraphicOverlay.getGraphics().add(lineGraphic);
        // add graphics overlay to the MapView
        mMapView.getGraphicsOverlays().add(lineGraphicOverlay);
    }

    /**
     * 添加地理位置的点
     * 百度地图拾取经纬度（ http://api.map.baidu.com/lbsapi/getpoint/index.html ）
     * 例：长沙 百度地图坐标点 ( 113.170224,28.040329 )
     * 第三个参数 使用wgs84模式 SpatialReferences.getWgs84()
     * ①WGS84  World Geodetic System 1984  GPS全球定位系统使用
     * ②WEB_MERCATOR 墨卡托(Mercator)投影
     */
    private void addPointOverLay() {
        Point pointGeometry = new Point(113.170224, 28.040329, SpatialReferences.getWgs84());
        // 简单的标记
        SimpleMarkerSymbol pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 5);
        // create graphic for point
        Graphic pointGraphic = new Graphic(pointGeometry);
        // create a graphic overlay for the point
        GraphicsOverlay pointGraphicOverlay = new GraphicsOverlay();
        // create simple renderer
        SimpleRenderer pointRenderer = new SimpleRenderer(pointSymbol);
        pointGraphicOverlay.setRenderer(pointRenderer);
        // add graphic to overlay
        pointGraphicOverlay.getGraphics().add(pointGraphic);
        // add graphics overlay to the MapView
        mMapView.getGraphicsOverlays().add(pointGraphicOverlay);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.resume();
    }

}
